// ignore_for_file: camel_case_types

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:verse_systems/pages/authentication/authentication_control.dart';
import 'package:verse_systems/res/colors.dart';
import 'package:verse_systems/utils/image_utils.dart';
import 'package:verse_systems/widgets/drawer_appbar.dart';
import 'package:verse_systems/widgets/sp_button.dart';

class AuthenticationBankInfo extends StatefulWidget {
  const AuthenticationBankInfo({super.key});

  @override
  State<AuthenticationBankInfo> createState() => _AuthenticationBankInfoState();
}

class _AuthenticationBankInfoState extends State<AuthenticationBankInfo> {
  final control = Get.find<AuthenticationControl>();
  final state = Get.find<AuthenticationControl>().state;

  @override
  void initState() {
    super.initState();
    control.getBankCardInfoList();
  }

  @override
  void dispose() {
    super.dispose();
    control.bankcard_code.text = "";
    control.bankcard_number.text = "";
    control.real_name.text = "";
    control.alipay_account.text = "";
    control.state.upload = false;
    control.state.alipay_code_url = "";
    control.state.alipay_code_path = "";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.system_background_color,
      appBar: DrawerAppbar(
        title: 'bank_infor'.tr,
      ),
      body: GetBuilder<AuthenticationControl>(
          init: control,
          builder: (_) {
            return GestureDetector(
                onTap: () {
                  FocusScope.of(context).unfocus();
                },
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      _information,
                      _bankInfo,
                      _alipayInfo,
                      _confirmNotice,
                      _next,
                    ],
                  ),
                ));
          }),
    );
  }

  Widget _bankTextField(
      {required final TextEditingController controller,
      required Function() onPressed,
      required String title,
      String? hintText,
      bool enabled = true,
      TextInputType keyboardType = TextInputType.text,
      IconData icon = Icons.mode_edit_outline}) {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w),
        width: MediaQuery.of(context).size.width,
        color: Colours.app_main_color,
        child: InkWell(
            onTap: onPressed,
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        color: Colours.app_forward_color,
                        fontSize: 16.sp,
                      ),
                    ),
                    Expanded(
                        child: TextField(
                      enabled: enabled,
                      maxLines: 1,
                      keyboardType: keyboardType,
                      controller: controller,
                      textAlign: TextAlign.right, //文本对齐方式
                      style: TextStyle(
                        fontSize: 15.sp,
                        color: Colours.app_black_text_color,
                      ),
                      decoration: InputDecoration(
                          suffixIcon: Icon(
                            icon,
                            color: Colours.app_line_color,
                            size: 18.w,
                          ),
                          border: InputBorder.none,
                          hintText: hintText ?? "please_enter".tr,
                          counterText: '',
                          hintStyle: TextStyle(
                            fontSize: 15.sp,
                          )),
                      showCursor: true,
                      cursorColor: Colours.app_main_background_color,
                      onChanged: (v) {},
                    ))
                  ],
                ),
                Container(
                  height: 1.w,
                  color: Colours.app_text_line_color,
                ),
              ],
            )));
  }

  Widget get _confirmNotice {
    if (state.upload == false) {
      return Container();
    }
    return Container(
      padding: EdgeInsets.symmetric(vertical: 15.w, horizontal: 15.w),
      margin: EdgeInsets.symmetric(
          horizontal: (MediaQuery.of(context).size.width - 350.w) / 2,
          vertical: 5.w),
      width: 350.w,
      child: Text(
        state.alipay_info_explain.toString(),
        textAlign: TextAlign.left,
        style: TextStyle(
          color: Colours.app_forward_color,
          fontSize: 12.sp,
        ),
      ),
    );
  }

  Widget get _alipayInfo {
    if (state.upload == false) {
      return Container();
    }
    return Padding(
        padding: EdgeInsets.symmetric(vertical: 10.w),
        child: InkWell(
          onTap: control.openAlbum,
          child: (state.alipay_code_url == "")
              ? Image.asset(
                  ImageUtils.getImgPath("alipay_upload"),
                  fit: BoxFit.fill,
                  width: 182.w,
                  height: 103.w,
                )
              : CachedNetworkImage(
                  fit: BoxFit.fill,
                  width: (MediaQuery.of(context).size.width - 65.w) / 2,
                  height: 106.w,
                  imageUrl: state.alipay_code_url.toString(),
                  placeholder: (context, url) => Transform.scale(
                    scale: 0.3,
                    child: const CircularProgressIndicator(
                      color: Colours.app_main_background_color,
                    ),
                  ),
                  errorWidget: (context, url, error) => const Icon(Icons.error),
                ),
        ));
  }

  Widget get _bankInfo {
    return Container(
      margin: EdgeInsets.fromLTRB(0, 12.w, 0, 0),
      child: Column(
        children: [
          _bankTextField(
              controller: control.bankcard_code,
              title: "bank".tr,
              hintText: "please_choice".tr,
              icon: Icons.arrow_forward_ios_rounded,
              enabled: false,
              onPressed: control.onPressedBank),
          _bankTextField(
              controller: control.bankcard_number,
              title: "bank_number".tr,
              keyboardType: TextInputType.number,
              onPressed: () {}),
          _bankTextField(
              controller: control.real_name,
              title: "cardholder_name".tr,
              onPressed: () {}),
          (state.upload == true)
              ? _bankTextField(
                  controller: control.alipay_account,
                  title: "alipay_account".tr,
                  onPressed: () {})
              : Container(),
        ],
      ),
    );
  }

  Widget get _next {
    double top = state.upload == false ? 300 : 100;
    return Container(
      margin: EdgeInsets.fromLTRB(0, top.w, 0, 35.w),
      width: 310.w,
      child: SPButton(
        minHeight: 50.w,
        fontSize: 13.sp,
        text: "next".tr,
        textColor: Colours.app_main_color,
        backgroundColor: Colours.app_main_background_color,
        radius: 25.w,
        onPressed: control.handleUploadBankCardInfo,
      ),
    );
  }

  Widget get _information {
    return Container(
        width: MediaQuery.of(context).size.width,
        color: Colours.app_main_color,
        padding: EdgeInsets.symmetric(horizontal: 24.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 24.w),
            Row(
              children: [
                Image.asset(
                  ImageUtils.getImgPath("banks"),
                  fit: BoxFit.fill,
                  width: 38.w,
                  height: 30.w,
                ),
                Text(
                  "bank_infor".tr,
                  style: TextStyle(
                      color: Colours.app_black_text_color,
                      fontSize: 24.sp,
                      fontWeight: FontWeight.w600),
                ),
              ],
            ),
            Container(
                margin: EdgeInsets.fromLTRB(0, 14.w, 0, 12.w),
                child: Text(
                  state.bank_notice_info,
                  style: TextStyle(
                    color: Colours.app_text_color,
                    fontSize: 12.sp,
                  ),
                )),
          ],
        ));
  }
}
